package com.tjbknb.ecui;

import java.io.IOException;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

public class ECUIActivity extends Activity {
	private Notification notification;
	private NotificationManager nManager;

	//debug log tag
	private static final String TAG = "Activity";
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		
		
		Log.i(TAG, "App starting..");
		
		
		//get root
		ECUIService.getRoot(getApplicationContext());
		
		
		
	
		//print available frequencies
		String freqsAvail = null;
		freqsAvail = ECUIService.getAvailableFreq();
		
		TextView availText = (TextView)findViewById(R.id.availabletext);
		availText.setText((CharSequence)(freqsAvail));
		
		TextView scriptText = (TextView)findViewById(R.id.scriptText);
		scriptText.setText("Script = " + Boolean.toString(ECUIService.script) + Integer.toString(ECUIService.appCounter));
		
			
			
		
		
		//printed cpu info
		new speedUpdate().execute();
			
		//turn off disable button
		Button disable = (Button) findViewById(R.id.button2);
		disable.setClickable(false);
		
	}

	
	
	//notification bar
	private void startNotification(){
		notification = new Notification();
		Intent notificationIntent = new Intent();
		PendingIntent contentIntent = PendingIntent.getActivity(this, (int)
				System.currentTimeMillis(),
				notificationIntent, 
				PendingIntent.FLAG_UPDATE_CURRENT);
		// We are using the custom view for our notification instead of the
		// setLatestEventInfo method
		RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.drawer);

		notification.contentView = contentView; 
		// Since we do not use the setLatestEventInfo, we have to set the
		// intent like this
		notification.contentIntent = contentIntent; 
		// Set the notification type to Ongoing Event, so that you will not
		// see the Clear button for this notification
		notification.flags = Notification.FLAG_ONGOING_EVENT; 
		// Set the notification icon, which is actually the LevelListDrawable
		// that you have created
		notification.icon = R.drawable.icon;

		nManager.notify(R.string.app_name, notification);
		// start the thread which updates the iconLevel at regular intervals
		//Thread thread = new Thread(this);
		//thread.start();
	}
	
	
	//buttons are created in xml files in res\layout
	//these are the methods called when the buttons are clicked
	
	//impatience button
	public void impatience(View view) throws IOException
	{
		ECUIService.userIsImpatient(getApplicationContext());
		Toast.makeText(getApplicationContext(), "You are impatient...", Toast.LENGTH_SHORT).show();
		
	}
	
	
	//enable button
	public void enableService(View view)
	{
		Button enable = (Button) findViewById(R.id.button1);
		enable.setClickable(false);
		Button disable = (Button) findViewById(R.id.button2);
		disable.setClickable(true);
		
		Intent enableIntent = new Intent(getApplicationContext(), ECUIService.class);
		startService(enableIntent);
	}
	
	
	//disable button
	public void disableService(View view)
	{
		Button disable = (Button) findViewById(R.id.button2);
		disable.setClickable(false);
		Button enable = (Button) findViewById(R.id.button1);
		enable.setClickable(true);
		
		Intent disableIntent = new Intent(getApplicationContext(), ECUIService.class);
		stopService(disableIntent);
	}
	
	
	//script buttons
	public void startScript(View view) {
		ECUIService.script = true;
	}
	
	public void stopScript(View view) {
		ECUIService.script = false;
	}
	
	
	
	
	//options menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	
	//handles clicking of options in menu
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//Log.d(TAG, Integer.toString(item.getItemId()));
		switch (item.getItemId()) {
		case R.id.settings:
			//Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
			Intent viewSettings = new Intent(ECUIActivity.this, settings.class);
			startActivity(viewSettings);
			return true;
		default:
			return false;
		}
	}
	
	

	
	

	//thread for continuous cpu freq updates
	public class speedUpdate extends AsyncTask<Void, String, Void>
	{
		
		//@Override
		protected Void doInBackground(Void... params) 
		{
			while (true)
			{
				String freqMhz = null;
				
				freqMhz = Integer.toString(Integer.parseInt(ECUIService.getCurrentFreq())/1000);
				
				//publish
				publishProgress(freqMhz);
				
				//pause
				try 
				{
					Thread.sleep(1000);
				} 
				catch (InterruptedException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
		}
		
		
		protected final void onProgressUpdate(String... progress) 
		{
			//find id of where cpu text is displayed, set the text to the found frequency
			TextView freqText = (TextView)findViewById(R.id.mhztext);
			freqText.setText((CharSequence)(progress[0])+" MHz");
		}
		
		
	}



}

